package com.xingshentech.ugv.operate.base;

import com.blankj.utilcode.util.LogUtils;
import com.xingshentech.ugv.operate.module.httpdata.DataEncryptInterceptor;
import com.xingshentech.ugv.operate.module.httpdata.DecryptionInterceptor;
import com.xingshentech.ugv.operate.module.utils.Utils;
import com.xingshentech.ugv.operate.sdk.SDKAdapter;
import com.xstech.library.app.BaseApp;
import com.xstech.library.rxjavahttp.BasicAuthInterceptor;
import com.xstech.library.rxjavahttp.CookieReadInterceptor;
import com.xstech.library.rxjavahttp.CookiesSaveInterceptor;
import com.xstech.library.rxjavahttp.HeaderInterceptor;
import com.xstech.library.rxjavahttp.InterceptorUtil;
import com.xstech.library.rxjavahttp.MoreBaseUrlInterceptor;
import com.xstech.library.utils.FileUtils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

public class ApplicationShare extends BaseApp {

    private static ApplicationShare app;


    public static ApplicationShare getApp() {
        return app;
    }

    public SDKAdapter getSdkAdapter() {
        return SDKAdapter.getInstance();
    }


    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    private static OkHttpClient mOkHttpClient;
    public static String fileserver_admin = "admin";
    public static String fileserver_pwd = "Xingshen#2017";

    @Override
    protected void onAppStart() {
        app = this;
//        XGPushConfig.enableDebug(this, true);
//        收集奔溃日志 sd卡下XinshenTech/carsh
//        UpdateAppUtils.init(this);
//        CrashHandler.getInstance().init(this, this);
//        AutoSizeConfig.getInstance().setBaseOnWidth(false).setCustomFragment(true);

        LogUtils.getConfig()
                .setLogSwitch(true)
                .setConsoleSwitch(true)
                .setLogHeadSwitch(false)
                .setLog2FileSwitch(true)
                .setDir(FileUtils.getAppFile() + "/log/")
                .setFilePrefix("" + Utils.getFirstInstallTime())
                .setSaveDays(7);
    }

    public static OkHttpClient initOKHttp() {
        if (mOkHttpClient == null) {
//            单例模式，确保 mOkHttpClient 只被创建一次，避免多次创建占用资源。
            HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new InterceptorUtil());
//            创建一个日志拦截器 logInterceptor，用于打印请求和响应的详细信息（请求 URL、参数、响应数据等）。
            logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//            HttpLoggingInterceptor.Level.BODY 代表打印完整的 请求 & 响应 body，适合调试网络请求。
            mOkHttpClient = new OkHttpClient.Builder()
//                     使用 OkHttpClient.Builder() 创建 HTTP 客户端，并配置各种参数和拦截器。
                    .addInterceptor(new CookiesSaveInterceptor())
                    .connectTimeout(15, TimeUnit.SECONDS)//设置连接超时时间 15 秒内无法建立连接，就终止请求
                    .readTimeout(15, TimeUnit.SECONDS)//设置读取超时时间 15 秒内没有数据返回，就终止请求。
                    .writeTimeout(15, TimeUnit.SECONDS)//设置写入超时时间 15 秒内数据没有成功写入，就终止请求
                    .addInterceptor(new MoreBaseUrlInterceptor())//这个拦截器会在请求时，动态替换 URL
                    .addNetworkInterceptor(logInterceptor)//添加日志拦截器 缓存 & 监控网络数据
                    .addInterceptor(new CookieReadInterceptor())//在发送请求时，自动读取之前保存的 Cookie，并附加到请求头
                    .addInterceptor(new HeaderInterceptor())// 用于 全局添加请求头 User-Agent（设备信息）
                    .addInterceptor(new DataEncryptInterceptor())
                    .addInterceptor(new DecryptionInterceptor())
                    .build();
        }
        return mOkHttpClient;
    }

    public static OkHttpClient initOKHttp2() {
        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new InterceptorUtil());
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new CookiesSaveInterceptor())
                .connectTimeout(15, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(15, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(15, TimeUnit.SECONDS)//设置写入超时时间
                .addInterceptor(logInterceptor)//添加日志拦截器
                .addInterceptor(new BasicAuthInterceptor(fileserver_admin, fileserver_pwd))

                .build();
        return client;
    }

}
